<?php

function getSelectedTeam($teamId) {
	$currentRound = getCurrentRound();
	$sql = "select tp.pickNumber, p.id as playerId, t.coachName, p.firstName, p.surname, tp.playerPosition, p.playerType, p.team, tp.isLti, tp.isLtiReplacement, ast.position from teamplayer tp";
	$sql .= " inner join team t on tp.teamId = t.id inner join player p on tp.playerId = p.id left join aflselectedteams ast on ast.playerId = p.id and ast.round = $currentRound where tp.teamId = $teamId and traded = false";
	$result = mysql_query($sql);
	
	$array = array();
	while($row = mysql_fetch_array($result)) {
		$array[] = getTeamPlayer($row);
	}
	
	
	return $array;
}

function clearPositions($teamId) {
	$sql = "UPDATE teamplayer set playerPosition = null where teamId = $teamId and traded = false;";
	
	mysql_query($sql);
	
}

function setPosition($teamId, $playerId, $position) {
	$sql = "UPDATE teamplayer set playerPosition = '$position' where playerId = $playerId and teamId = $teamId and traded = false;";
	
	mysql_query($sql);
	
}

function updateReserves($teamId) {
	$sql = "UPDATE teamplayer set playerPosition = 'RES' where teamId = $teamId and traded = false and playerPosition is null;";
	
	mysql_query($sql);
	
}

function isTeamPlayerValid($teamId, $playerId) {
	$sql = "select count(*) as count from teamplayer where teamId = $teamId and playerId = $playerId and traded = false";
	$result = mysql_query($sql);
	$row = mysql_fetch_array($result);
        $returnResult = true;
        if ($row['count'] == 0) {
           $returnResult = false;
        }
		
	
	return $returnResult;
}

function getUsedPositions($teamId) {
	$sql = "select playerPosition from teamplayer where teamId = $teamId and traded = false;";
	
	$result = mysql_query($sql);
	
	$array = array();
	while($row = mysql_fetch_array($result)) {
		$array[] = $row['playerPosition'];
	}	
		
	
	return $array;
}

function getRoundScoreForTeam($teamId, $round, $leagueId) {
	$sql = "select pfr.playerId as playerId, p.id, p.firstName, p.surname, p.team, p.playerType, ps.score, pfr.replacementPlayerId, pfr.replacementDescription, pfr.emergency, ast.position,";
	$sql .= " pfr.playerPosition, pfr.isInValid, p.isKPD, tp.traded from teamplayer tp left join playerforround pfr on pfr.playerId = tp.id and pfr.round = $round inner join playerscore ps on tp.playerId = ps.playerId";
	$sql .= " and ps.round = $round inner join player p on ps.playerId = p.id left join aflselectedteams ast on ast.playerId = p.id and ast.round = $round ";	
	$sql .= " where tp.teamId = $teamId";
	
	$result = mysql_query($sql);
	
	$array = array();
	$replacements = array();
	$emergencies = array();
	$nonScoringPlayers = array();
	while($row = mysql_fetch_array($result)) {
		$playerScore = new PlayerScore();
		$player = new Player();
		$player->id = $row['id'];
		$player->playerFirstName = $row['firstName'];
		$player->playerSurname = $row['surname'];
		$player->team = $row['team'];
		$player->playerType = $row['playerType'];
		$playerScore->player = $player;
		$playerScore->playerId = $row['playerId'];
		$playerScore->score = $row['score'];
		$replacementPlayerId = $row['replacementPlayerId'];
		$playerScore->emergency = $row['emergency'];
		$playerScore->replacementDescription = $row['replacementDescription'];		
		$playerScore->positionInAFLTeam = $row['position'];
		$playerScore->playerPosition = $row['playerPosition'];
		$playerScore->isInValid = $row['isInValid'];
		$playerScore->isKPD = $row['isKPD'];		
		$playerScore->traded = $row['traded'];
		if ($replacementPlayerId != null) {
			$playerScore->replacementPlayerId = $replacementPlayerId;
			$replacements[] = $playerScore;
		}
		else if ($playerScore->emergency) {
			$emergencies[] = $playerScore;
		}
		else if ($playerScore->playerId == null) {
			$nonScoringPlayers[] = $playerScore;
		} 
		else {
			$array[] = $playerScore;
		}
	}
	
	$settings = getSystemSettings($leagueId);
	$penalty = getPenalty($teamId, $round, false, $settings);
	if ($penalty != 0) {
		$playerScore = new PlayerScore();
		$player = new Player();
		$player->playerFirstName = "Penalty";
		$playerScore->player = $player;
		$playerScore->score = $penalty;
		
		$array[] = $playerScore;
	}
	
	$count = count($replacements);
	$count2 = count($emergencies);
	for ($i = 0; $i < $count; $i++) {
		for ($j = 0; $j < $count2; $j++) {
			if ($emergencies[$j] != null && $replacements[$i]->replacementPlayerId == $emergencies[$j]->playerId) {
				$emergencies[$j]->replacementFor = $replacements[$i]->player;
				$emergencies[$j]->replacementDescription = $replacements[$i]->replacementDescription;
				$array[] = $emergencies[$j];
				$emergencies[$j] = null; 
			}
		}			
	}
	
	for ($j = 0; $j < $count2; $j++) {
		if ($emergencies[$j] != null) {
			$nonScoringPlayers[] = $emergencies[$j];
		}
	}
	
	usort($array, "comparePlayerScore");
	usort($nonScoringPlayers, "comparePlayerScore");
	
	$playerScores = new PlayerScores();
	$playerScores->playerScores = $array;
	$playerScores->nonScoringPlayers = $nonScoringPlayers;
	
	return $playerScores;
}

?>